package com.lanou.connotationjoke.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.activity.WebViewActivity;
import com.lanou.connotationjoke.db.HomeCollectEntity;
import com.lanou.connotationjoke.db.HomeCollectEntityDao;
import com.lanou.connotationjoke.entity.DataDetialEty;
import com.lanou.connotationjoke.entity.JsonEty;
import com.lanou.connotationjoke.tool.DaoSingleton;
import com.lanou.connotationjoke.tool.MyAppllication;
import com.lanou.connotationjoke.tool.ShareSingleton;
import com.lanou.connotationjoke.tool.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/13.
 */
public class HomeJokeListViewAdapter extends BaseAdapter{
    private Context context;
    private List<DataDetialEty> datas;
    private HomeCollectEntityDao homeCollectEntityDao
            = DaoSingleton.getInstance().getHomeCollectEntityDao();
    public HomeJokeListViewAdapter(Context context, List<DataDetialEty> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (datas.get(position).getGroup()!=null) {
            JokeViewHolder jokeViewHolder = null;
            JokeWithCommentViewHolder commentViewHolder = null;
            int type = getItemViewType(position);
            if (convertView == null) {
                if (type == 0) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_recycleview_home_joke, null);
                    jokeViewHolder = new JokeViewHolder(convertView);
                    convertView.setTag(jokeViewHolder);
                } else {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_recycleview_home_joke_comment, null);
                    commentViewHolder = new JokeWithCommentViewHolder(convertView);
                    convertView.setTag(commentViewHolder);
                }
            } else {
                if (type == 0) {
                    jokeViewHolder = (JokeViewHolder) convertView.getTag();
                } else {
                    commentViewHolder = (JokeWithCommentViewHolder) convertView.getTag();
                }
            }
            if (type == 0) {
                jokeViewHolder.diggTv.setText("" + datas.get(position).getGroup().getDigg_count());
                jokeViewHolder.contentTv.setText(datas.get(position).getGroup().getContent());
                jokeViewHolder.userNameTv.setText(datas.get(position).getGroup().getUser().getName());
                jokeViewHolder.categoryName_Tv.setText(datas.get(position).getGroup().getCategory_name());
                jokeViewHolder.buryTv.setText("" + datas.get(position).getGroup().getBury_count());
                jokeViewHolder.hotCommentTv.setText("" + datas.get(position).getGroup().getComment_count());
                jokeViewHolder.sendTv.setText("" + datas.get(position).getGroup().getShare_count());
                jokeViewHolder.userPictureIv.setDefaultImageResId(R.mipmap.ic_launcher);
                jokeViewHolder.userPictureIv.setErrorImageResId(R.mipmap.ic_launcher);
                jokeViewHolder.userPictureIv.setImageUrl(datas.get(position).getGroup().getUser().getAvatar_url()
                        , VolleySingleton.getImageLoader());
                jokeViewHolder.contentTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (MyAppllication.hasNetwork()) {
                            String url = "http://neihanshequ.com/p" + datas.get(position).getGroup().getShare_url().substring(37);
                            WebViewActivity.goToWebViewActivity(context, url);
                        } else {
                            Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                jokeViewHolder.shareIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (MyAppllication.hasNetwork()){
                            String title = "超级搞笑";
                            String titleurl = "http://neihanshequ.com/p" + datas.get(position).getGroup().getShare_url().substring(37);
                            String content = datas.get(position).getGroup().getContent();
                            String imageurl = datas.get(position).getGroup().getUser().getAvatar_url();
                            ShareSingleton.getInstance().shareOnQQ(title, titleurl, content, imageurl);
                        }else {
                            Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                jokeViewHolder.collectTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        insertDataToHomeCollectDao(datas.get(position));
                        Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
                    }
                });

            } else {
                commentViewHolder.userNameTv.setText(datas.get(position).getGroup().getUser().getName());
                commentViewHolder.contentTv.setText(datas.get(position).getGroup().getContent());
                commentViewHolder.categoryName_Tv.setText(datas.get(position).getGroup().getCategory_name());
                commentViewHolder.diggTv.setText("" + datas.get(position).getGroup().getDigg_count());
                commentViewHolder.buryTv.setText("" + datas.get(position).getGroup().getBury_count());
                commentViewHolder.hotCommentTv.setText("" + datas.get(position).getGroup().getComment_count());
                commentViewHolder.sendTv.setText("" + datas.get(position).getGroup().getShare_count());
                commentViewHolder.userPictureIv.setDefaultImageResId(R.mipmap.ic_launcher);
                commentViewHolder.userPictureIv.setErrorImageResId(R.mipmap.ic_launcher);
                commentViewHolder.userPictureIv.setImageUrl(datas.get(position).getGroup().getUser().getAvatar_url()
                        , VolleySingleton.getImageLoader());
                commentViewHolder.commentUserNameTv.setText(datas.get(position).getComments().get(0).getUser_name());
                commentViewHolder.commentDiggCountTv.setText("" + datas.get(position).getComments().get(0).getDigg_count());
                commentViewHolder.commentContentTv.setText("" + datas.get(position).getComments().get(0).getText());
                commentViewHolder.commentUserPictureIv.setDefaultImageResId(R.mipmap.ic_launcher);
                commentViewHolder.commentUserPictureIv.setErrorImageResId(R.mipmap.ic_launcher);
                commentViewHolder.commentUserPictureIv.setImageUrl(datas.get(position).getComments().get(0).getAvatar_url(), VolleySingleton.getImageLoader());
                commentViewHolder.contentTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (MyAppllication.hasNetwork()) {
                            String url = "http://neihanshequ.com/p" + datas.get(position).getGroup().getShare_url().substring(37);
                            WebViewActivity.goToWebViewActivity(context, url);
                        } else {
                            Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                commentViewHolder.shareIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (MyAppllication.hasNetwork()){
                            String title = "超级搞笑";
                            String titleurl = "http://neihanshequ.com/p" + datas.get(position).getGroup().getShare_url().substring(37);
                            String content = datas.get(position).getGroup().getContent();
                            String imageurl = datas.get(position).getGroup().getUser().getAvatar_url();
                            ShareSingleton.getInstance().shareOnQQ(title, titleurl, content, imageurl);
                        }else {
                            Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                commentViewHolder.collectTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        insertDataToHomeCollectDao(datas.get(position));
                        Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.get(position).getComments()==null||
                datas.get(position).getComments().size()==0)
        return 0;
        else {
            return 1;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    public void refreshData(JsonEty jsonEty){
        this.datas = jsonEty.getData().getData();
        notifyDataSetChanged();
    }

    private void insertDataToHomeCollectDao(DataDetialEty dataDetialEty){
        String avatar_url = dataDetialEty.getGroup().getUser().getAvatar_url();
        String name = dataDetialEty.getGroup().getUser().getName();
        String content = dataDetialEty.getGroup().getContent();
        String category_name = dataDetialEty.getGroup().getCategory_name();
        int digg_count = dataDetialEty.getGroup().getDigg_count();
        int bury_count = dataDetialEty.getGroup().getBury_count();
        int comment_count = dataDetialEty.getGroup().getComment_count();
        int share_count = dataDetialEty.getGroup().getShare_count();
        String share_url = dataDetialEty.getGroup().getShare_url();

        String uri = null;
        String coverImageUri = null;
        String mp4Url = null;
        int playCount = 0;


        boolean differernt = true;
        List<HomeCollectEntity> homeCollectEntities = homeCollectEntityDao.queryBuilder().list();
        if (homeCollectEntities != null){
            for (HomeCollectEntity homeCollectEntity : homeCollectEntities){
                if (homeCollectEntity.getShare_url() == share_url){
                    differernt = false;
                }
            }
        }
        if (differernt){
            homeCollectEntityDao.insert(new HomeCollectEntity(homeCollectEntityDao.count(),avatar_url,name,content
                    ,category_name,uri,coverImageUri,mp4Url,playCount,digg_count,bury_count
                    ,comment_count,share_count,share_url));
        }
    }

    class JokeViewHolder{
        NetworkImageView userPictureIv;
        TextView userNameTv;
        TextView contentTv;
        TextView categoryName_Tv;
        TextView diggTv;
        TextView buryTv;
        TextView hotCommentTv;
        TextView sendTv;
        ImageView shareIv;
        TextView collectTv;

        public JokeViewHolder(View itemView) {
            userPictureIv = (NetworkImageView) itemView.findViewById(R.id.iv_user_picture);
            userNameTv = (TextView) itemView.findViewById(R.id.tv_user_name);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            categoryName_Tv = (TextView) itemView.findViewById(R.id.tv_category_name);
            diggTv = (TextView) itemView.findViewById(R.id.tv_digg_count);
            buryTv = (TextView) itemView.findViewById(R.id.tv_bury_count);
            hotCommentTv = (TextView) itemView.findViewById(R.id.tv_hot_comment_count);
            sendTv = (TextView) itemView.findViewById(R.id.tv_send_count);
            shareIv = (ImageView) itemView.findViewById(R.id.iv_share_home_joke);
            collectTv = (TextView) itemView.findViewById(R.id.tv_collect);
        }
    }

    class JokeWithCommentViewHolder{
        NetworkImageView userPictureIv;
        TextView userNameTv;
        TextView contentTv;
        TextView categoryName_Tv;
        TextView diggTv;
        TextView buryTv;
        TextView hotCommentTv;
        TextView sendTv;
        NetworkImageView commentUserPictureIv;
        TextView commentUserNameTv;
        TextView commentDiggCountTv;
        TextView commentContentTv;
        ImageView shareIv;
        TextView collectTv;

        public JokeWithCommentViewHolder(View itemView) {
            userPictureIv = (NetworkImageView) itemView.findViewById(R.id.iv_user_picture);
            userNameTv = (TextView) itemView.findViewById(R.id.tv_user_name);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            categoryName_Tv = (TextView) itemView.findViewById(R.id.tv_category_name);
            diggTv = (TextView) itemView.findViewById(R.id.tv_digg_count);
            buryTv = (TextView) itemView.findViewById(R.id.tv_bury_count);
            hotCommentTv = (TextView) itemView.findViewById(R.id.tv_hot_comment_count);
            sendTv = (TextView) itemView.findViewById(R.id.tv_send_count);
            commentUserPictureIv = (NetworkImageView) itemView.findViewById(R.id.iv_comment_user_picture);
            commentUserNameTv = (TextView) itemView.findViewById(R.id.tv_comment_user_name);
            commentDiggCountTv = (TextView) itemView.findViewById(R.id.tv_comment_digg_count);
            commentContentTv = (TextView) itemView.findViewById(R.id.tv_comment_content);
            shareIv = (ImageView) itemView.findViewById(R.id.iv_share_home_joke_comment);
            collectTv = (TextView) itemView.findViewById(R.id.tv_collect);
        }
    }
}
